package com.xuanyuan.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xuanyuan.model.Departments;
import com.xuanyuan.model.User;
import com.xuanyuan.service.UserService;
import com.xuanyuan.util.PageInfo;
import com.xuanyuan.vo.UserVo;

@Component("userAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@RemoteProxy(name = "userAction")
public class UserAction extends ActionSupport{
     UserService userService;
     
     
     @RemoteMethod
     public Boolean isLogin(String userName, String password){
    	 boolean flag = userService.isLogin(userName, password);
    	 if(flag == true){
    		 ActionContext.getContext().getSession().put("userName", userName);
    		 return flag;
    	 } 
    	 return flag;
    	 
     }
     
     @RemoteMethod
     public void addUser(String userName, String password, String department, String email, String mobile, String name) {
    	 User user = new User();
    	 user.setDepartment(department);
    	 user.setEmail(email);
    	 user.setName(name);
         user.setPassword(password);
         user.setPhone(mobile);
         user.setUserName(userName);
         user.setPer("0");
    	 userService.addUser(user);
     }
     
     @RemoteMethod
     public Map<String, Object> listUser(PageInfo pageInfo) {
    	 System.out.println("come in");
    	 Map<String, Object> map = new HashMap<String, Object>();
    	 List<UserVo> userVos = userService.listUser(pageInfo);
    	 
    	 map.put("userVos", userVos);
    	 map.put("pageInfo", pageInfo);
    	 return map;
     }
     
     @RemoteMethod
     public UserVo loadUser(){
    	 String userName = (String) ActionContext.getContext().getSession().get("userName");
    	 return userService.selectUser(userName);
     }
 
     @RemoteMethod
     public UserVo loadUserVo(Integer userId){
    	 return userService.getUserVo(userId);
     }
     
     
     @RemoteMethod
     public void delUser(Integer userId){
    	 userService.delUser(userService.getUsers(userId));
     }
    
     
     @RemoteMethod
	 public UserVo selectUser(String userName) {
    	 System.out.println("---->"+userService.selectUser(userName));
		return userService.selectUser(userName);
	 }

	 @Resource
	 public void setUserService(UserService userService) {
		 this.userService = userService;
	 }
	 
	 @RemoteMethod
	 public void updateUser(Integer userId, String userName, String name,
				String department, String phone, String email) {
		     userService.updateUser(userId, userName, name, department, phone, email);
	 }

   @RemoteMethod
   public UserVo loadUserById(Integer userId){
	   return userService.getUserVo(userId);
   }

   @RemoteMethod
   public void addUserToDepartment(String realName, String department, String mobile){
	   Departments user = new Departments();
	   user.setDepartmentName(department);
	   user.setPhoneNumber(mobile);
	   user.setUserName(realName);
	   userService.addUserToDepartment(user);
   }
     
   @RemoteMethod
   public Boolean updatePass(String password,String oldPass){
	   String username = (String) ActionContext.getContext().getSession().get("userName");
	   User u = userService.selectPass(oldPass, username);
	   if(u != null){
		   userService.updatePass(password, username);
		   return true;
	   } else {
		   return false;
	   }
   }
   
	 @RemoteMethod
   public Map<String, Object> loadUserByName(String username, PageInfo pageInfo){
		 System.out.println("--------");
	   List<UserVo> users = userService.getListUserByName(username, pageInfo);
	   Map<String, Object> map = new HashMap<String, Object>();
	   map.put("pageInfo", pageInfo);
	   map.put("userVos", users);
	   return map;
   }
}
